package edu.ID.mymaze;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class GridGame {
	private static int time=15;
	private static boolean timeTrue=false;
	public static void main(String[] args) {
		int loop=0;
		while(loop<3)
		{	//timer reset per round
			time=15;
			Scanner myScanner = new Scanner(System.in);
			//instructions
			System.out.println("Use the WASD keys to move your hero. Your hero is the *, your goal is the X, and | are walls.");
			System.out.println("You have 15 seconds to complete each maze.");
			// n and m ask user for dimensions of array
			System.out.println("What dimensions should your array have?");
			int n = myScanner.nextInt();
			int m = myScanner.nextInt();
			String[][] myArray = new String[n][m];
			//timer
			Timer timer = new Timer();
			timer.schedule(new TimerTask() {
				public void run() {
					/* Write code here */
					time--;
					if(time==0)
					{
						timeTrue=true;
					}

				}
			}, 0, 1000);


			//makes array
			for(int r=0; r<myArray.length; r++)
			{
				for(int c=0; c<myArray[0].length; c++)
				{
					myArray[r][c]=" ";
				}
			}

			// this prints hero
			int heroRow = (int)(Math.random()*n);
			int heroCol = (int)(Math.random()*m);
			myArray[heroRow][heroCol]="*";

			// prints walls
			for(int wr=0; wr<(n*m)/4; wr++)
			{
				int wallRow = (int)(Math.random()*(n-2))+1;
				int wallCol = (int)(Math.random()*m) ;
				if(myArray[wallRow][wallCol].equals(" "))
				{
					myArray[wallRow][wallCol]="|";

				}
				else
				{
					wr= wr-1;
				}
			}
			//makes goal
			int goalRow = (int)(Math.random()*n);
			int goalCol = (int)(Math.random()*m);
			myArray[goalRow][goalCol]="X";
			printArray(myArray);
			//repeats the printing of the array and hero movement until round/game end
			while(changeArray(myArray)==false)
			{
				printArray(myArray);
			}
			if(timeTrue==true)
				loop=3;
			loop++;
		}
		//end text
		if(timeTrue==true)
			System.out.println("Sorry, you lose. You ran out of time.");
		else
			System.out.println("You win!");
	}
	//prints array
	public static void printArray(String[][] refresh)
	{
		for(int r=0; r<refresh.length; r++)
		{
			for(int c=0; c<refresh[0].length; c++)
			{
				//changeArray(refresh);
				System.out.print(refresh[r][c]);
			}
			System.out.println();


		}
	}
	//moves the star unless it is out of bounds, on a wall, or out of time
	public static boolean changeArray(String refresh[][])
	{
		//movement choice
		Scanner myScanner = new Scanner(System.in);
		String action = myScanner.next();

		//finds star then does your action
		for(int r=0; r<refresh.length; r++) {
			for(int c=0; c<refresh[0].length; c++) {
				if (!refresh[r][c].equals("*")) continue;
				if(timeTrue==true) 
				{
					return true;					
				}
				//move up
				if(action.equals("w")) {
					if (r-1 >= 0) {
						if(refresh[r-1][c].equals(" ")) {
							refresh[r-1][c]="*";
							refresh[r][c]=" ";
						}
						else if(refresh[r-1][c].equals("X"))
							return true;
						else {
							System.out.println("That action is invalid.");
							changeArray(refresh);
						}
					}
					else {
						System.out.println("That is not a valid action.");
						printArray(refresh);
					}
				}
				//move left
				if(action.equals("a")) {
					if((c-1)>=0) {
						if(refresh[r][c-1].equals(" ")) {
							refresh[r][c-1]="*";
							refresh[r][c]=" ";
						}
						else if(refresh[r][c-1].equals("X"))
							return true;
						else
						{
							System.out.println("That action is invalid.");
							changeArray(refresh);
						}
					}
					else {
						System.out.println("That is not a valid action.");
						printArray(refresh);
					}

				}
				//move down
				if(action.equals("s")){
					if((r+1)<refresh.length) {
						if(refresh[r+1][c].equals(" "))
						{
							refresh[r+1][c]="*";
							refresh[r][c]=" ";
							action="";
						}
						else if(refresh[r+1][c].equals("X"))
							return true;
						else
						{
							System.out.println("That action is invalid.");
							changeArray(refresh);
							action="";
						}
					}
					else {
						System.out.println("That is not a valid action.");
						printArray(refresh);
					}
				}
				//move to right
				if(action.equals("d"))	{
					if((c+1)<refresh[0].length) {
						if(refresh[r][c+1].equals(" "))
						{
							refresh[r][c+1]="*";
							refresh[r][c]=" ";
							action="";
						}
						else if(refresh[r][c+1].equals("X"))
							return true;
						else
						{
							System.out.println("That action is invalid.");
							changeArray(refresh);
							action="";
						}	
					}
					else {
						System.out.println("That is not a valid action.");
						printArray(refresh);
					}
				}
			}
		}
		//will cause to repeat
		return false;
	}
}

